package com.brkj.test;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import com.brkj.httpInterface.BaseAjaxParams;
import com.brkj.httpInterface.HttpInterface;
import com.brkj.main.R;
import com.brkj.model.DS_Vote;
import com.brkj.util.MyAjaxCallBack;
import com.brkj.util.MyApplication;
import com.brkj.util.view.BaseCoreActivity;
import com.brkj.util.view.BaseListFragment;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import net.tsz.afinal.FinalHttp;

import java.lang.reflect.Type;
import java.util.List;

/**
 * Created by colin.ding on 2014/5/7.
 */
public class VoteFragment extends BaseListFragment {

    private BroadcastReceiver br;
    private List<DS_Vote> voteList;
    protected VoteAdapter adapter;

    public VoteFragment(Context context) {
        super(context);
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //注册 调查问卷做完了
//        IntentFilter filter = new IntentFilter("com.brkj.votehavedone");
//        br = new BroadcastReceiver() {
//
//            @Override
//            public void onReceive(Context arg0, Intent arg1) {
//                if(voteList!=null){
//                    for(int i=0; i<voteList.size(); i++){
//                        if(voteList.get(i).getVotePaperID()
//                                == Integer.valueOf(arg1.getStringExtra("voteID"))){
//                            voteList.remove(i);
//                            adapter.notifyDataSetChanged();
//                            return;
//                        }
//                    }
//                }
//
//            }
//        };
//
//        registerReceiver(br, filter);
    }

    @Override
    protected void fillView() {
        BaseAjaxParams params = new BaseAjaxParams();
        params.put(HttpInterface.HIF_GetVoteList.params.UserID, MyApplication.myUserID);

        FinalHttp fh = new FinalHttp();
        fh.post(HttpInterface.HIF_GetVoteList.address, params, new MyAjaxCallBack<Object>((BaseCoreActivity)mContext) {


            @Override
            public void onSuccess(Object t) {
                // TODO Auto-generated method stub
                super.onSuccess(t);

                Gson gson = new Gson();
                Type listType = new TypeToken<List<DS_Vote>>() {
                }.getType();
                voteList = gson.fromJson((String) t, listType);

                if ((voteList != null) && (voteList.size() > 0)) {
                    adapter = new VoteAdapter(mContext, voteList);
                    setListAdapter(adapter);

                    root.findViewById(R.id.noData).setVisibility(View.GONE);
                } else {
                    root.findViewById(R.id.noData).setVisibility(View.VISIBLE);
                }
            }

        });
    }


    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
    }
}